home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 14 assemblies / appdomaindemo / module1.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  6.5 KB  |  175 lines

  1. Option Strict Off
  2.  
  3. Module Module1
  4.  
  5.     ' Set this to True to generate the ShowAppDomainData.exe assembly
  6.     ' (but requires manual renaming of the exe file)
  7. #Const GEN_SHOWAPPDOMAINDATAEXE = False
  8.  
  9.     Sub Main()
  10.  
  11. #If GEN_SHOWAPPDOMAINDATAEXE Then
  12.         ' If inside the ShowAppDomainData.Exe program, show
  13.         ' AppDomain properties and exit.
  14.         TestAppDomainProperties
  15.         Exit Sub
  16. #End If
  17.  
  18.         'TestAppDomainProperties()
  19.         'TestCreateDomain()
  20.         TestCreateInstance()
  21.         'TestProcessExit()
  22.         'TestUnhandledException()
  23.  
  24.         ' These statements are usuful when running inside Visual Studio.NET
  25.         Console.WriteLine("")
  26.         Console.WriteLine(">>> Press Enter to terminate the program <<<")
  27.         Console.ReadLine()
  28.     End Sub
  29.  
  30.     ' this procedure displays AppDomain properties
  31.  
  32.     Sub TestAppDomainProperties()
  33.         ' Get a reference to the running AppDomain.
  34.         Dim ad As AppDomain = AppDomain.CurrentDomain
  35.  
  36.         ' Print main properties.
  37.         Console.WriteLine("----- AppDomain Data ------------------")
  38.         Console.WriteLine("BaseDirectory = {0}", ad.BaseDirectory)
  39.         Console.WriteLine("FriendlyName = {0}", ad.FriendlyName)
  40.         Console.WriteLine("RelativeSearchPath = {0}", ad.RelativeSearchPath)
  41.         Console.WriteLine("ShadowCopyFiles = {0}", ad.ShadowCopyFiles)
  42.         Console.WriteLine("CurrentThreadId = {0}", AppDomain.GetCurrentThreadId)
  43.  
  44.         ' Display additional information from the AppDomainSetup object.
  45.         Dim ads As AppDomainSetup = ad.SetupInformation
  46.  
  47.         Console.WriteLine("ApplicationName = {0}", ads.ApplicationName)
  48.         Console.WriteLine("CachePath = {0}", ads.CachePath)
  49.         Console.WriteLine("ConfigurationFile = {0}", ads.ConfigurationFile)
  50.         Console.WriteLine("DisallowPublisherPolicy = {0}", ads.DisallowPublisherPolicy)
  51.         Console.WriteLine("LoaderOptimization = {0}", ads.LoaderOptimization)
  52.         Console.WriteLine("PrivateBinPath = {0}", ads.PrivateBinPath)
  53.         Console.WriteLine("ShadowCopyFiles = {0}", ads.ShadowCopyFiles)
  54.  
  55.         ' Display the list of loaded files
  56.         Dim asm As Reflection.Assembly
  57.         Console.WriteLine("Assemblies loaded in current domain")
  58.         For Each asm In ad.GetAssemblies
  59.             Console.WriteLine("  " & asm.FullName)
  60.         Next
  61.         Console.WriteLine("----- End of AppDomain Data ------------")
  62.     End Sub
  63.  
  64.     ' this procedure tests AppDomain creation
  65.  
  66.     ' IMPORTANT: this routine assumes that you have an EXE named
  67.     ' ShowAppDomainData.exe in the BIN directory. If you don't, you
  68.     ' can copy the file found in the project's main directory.
  69.  
  70.     ' This executable file can be created by setting the
  71.     ' GEN_SHOWAPPDOMAINDATAEXE constant to True, compiling the
  72.     ' source and then renaming the EXE to ShowAppDomainData.exe
  73.  
  74.     Sub TestCreateDomain()
  75.         ' Prepare setup info
  76.         Dim ads As New AppDomainSetup()
  77.         ads.ApplicationName = "NewApplication"
  78.         ads.ApplicationBase = "c:\windows\temp"
  79.         ads.PrivateBinPath = "bins;assemblies"
  80.         ' Create the new AppDomain
  81.         Dim ad As AppDomain = AppDomain.CreateDomain("NewAppDomain", Nothing, ads)
  82.  
  83.         ' Run an assembly inside the appdomain
  84.         ad.ExecuteAssembly("ShowAppDomainData.exe")
  85.  
  86.         ' Unload the AppDomain
  87.         AppDomain.Unload(ad)
  88.     End Sub
  89.  
  90.     ' this procedure tests the CreateInstanceFrom method
  91.  
  92.     ' IMPORTANT: it relies on the presence of the TestAssembly.Dll
  93.     ' file in the BIN subdirectory. If this file isn't there, you can copy
  94.     ' it from the main project's directory, or you can recompile the
  95.     ' TestAssembly project
  96.  
  97.     Sub TestCreateInstance()
  98.         ' Create the new AppDomain
  99.         Dim ad As AppDomain = AppDomain.CreateDomain("NewAppDomain")
  100.         ' Load 
  101.         Dim oh As System.Runtime.Remoting.ObjectHandle
  102.         Dim o As Object
  103.  
  104.         oh = ad.CreateInstanceFrom("TestAppDomain.Dll", "TestAppDomain.AppDomainData")
  105.         o = oh.Unwrap
  106.         Console.WriteLine("ThreadID = {0}    (current={1})", o.ThreadID, AppDomain.GetCurrentThreadId)
  107.         Console.WriteLine("AppName = {0}", o.AppName)
  108.         Console.WriteLine("ExeName= {0}", o.ExeName)
  109.  
  110.         oh = ad.CreateInstanceFrom("TestAppDomain.Dll", "TestAppDomain.AppDomainData2")
  111.         o = oh.Unwrap
  112.         Console.WriteLine("ThreadID = {0}    (current={1})", o.ThreadID, AppDomain.GetCurrentThreadId)
  113.         Console.WriteLine("AppName = {0}", o.AppName)
  114.         Console.WriteLine("ExeName= {0}", o.ExeName)
  115.     End Sub
  116.  
  117.     ' this procedure tests the ProcessExit event 
  118.  
  119.     Dim WithEvents CurrAppDomain As AppDomain
  120.     Public AppIsExiting As Boolean
  121.  
  122.     Sub TestProcessExit()
  123.         ' Prepare to trap AppDomain events.
  124.         CurrAppDomain = AppDomain.CurrentDomain
  125.         ' Create a DataClass instance that will be collected right now
  126.         Dim dc As New DataClass()
  127.         dc = Nothing
  128.         GC.Collect()
  129.         ' Create an instance of DataClass that will be collected when the application exits.
  130.         Dim dc2 As New DataClass()
  131.     End Sub
  132.  
  133.     Public Sub CurrAppDomain_ProcessExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles CurrAppDomain.ProcessExit
  134.         Console.WriteLine("Exiting...")
  135.         AppIsExiting = True
  136.     End Sub
  137.  
  138.     ' this procedure tests unhandled exceptions on secondary
  139.     ' and main threads, and the UnhandledException event
  140.  
  141.     Sub TestUnhandledException()
  142.         ' Prepare to trap AppDomain events.
  143.         CurrAppDomain = AppDomain.CurrentDomain
  144.  
  145.         ' Cause an exception on a secondary thread
  146.         Dim t As New Threading.Thread(AddressOf ThrowException)
  147.         t.Start()
  148.         Threading.Thread.Sleep(500)
  149.  
  150.         ' Cause a fatal exception on the main thread
  151.         Throw New IndexOutOfRangeException()
  152.  
  153.     End Sub
  154.  
  155.     Sub ThrowException()
  156.         Throw New DivideByZeroException()
  157.     End Sub
  158.  
  159.     Sub CurrAppDomain_UnhandledException(ByVal sender As Object, ByVal e As System.UnhandledExceptionEventArgs) Handles CurrAppDomain.UnhandledException
  160.         ' get a reference to the exception
  161.         Dim ex As Exception = DirectCast(e.ExceptionObject, Exception)
  162.         ' Show information on the current exception.
  163.         Console.WriteLine("{0}  (IsTerminating={1})", ex.Message, e.IsTerminating)
  164.     End Sub
  165. End Module
  166.  
  167. Class DataClass
  168.     Public ID As Integer
  169.  
  170.     Protected Overrides Sub Finalize()
  171.         Console.WriteLine("Finalizing DataClass (AppIsExiting is {0})", AppIsExiting)
  172.     End Sub
  173. End Class
  174.  
  175.